package TLF 
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flashx.textLayout.container.ContainerController;
	import flashx.textLayout.edit.EditManager;
	import flashx.textLayout.edit.SelectionManager;
	import flashx.textLayout.elements.InlineGraphicElement;
	import flashx.textLayout.elements.LinkElement;
	import flashx.textLayout.elements.ParagraphElement;
	import flashx.textLayout.elements.SpanElement;
	import flashx.textLayout.elements.TextFlow;
	import flashx.textLayout.formats.LineBreak;
	
	
	public class ChatRender extends Sprite
	{
		private var userTextFlow:TextFlow;
		public function ChatRender() 
		{
           var editMode:EditManager = new EditManager();			
			userTextFlow.interactionManager = editMode;

			var container:Sprite=new Sprite();
				this.addChild(container);
			var cc:ContainerController = new ContainerController(container, 400,55);			
             userTextFlow.flowComposer.addController(cc);
             userTextFlow.lineBreak = LineBreak.EXPLICIT;　//一行
			 userTextFlow.flowComposer.updateAllControllers();
		}
		
		
		private var _BorderLine:Sprite = new Sprite();
		public function set border(value:Boolean):void {
			var cc:ContainerController = userTextFlow.flowComposer.getControllerAt(0)   
			if (value) {
				_BorderLine.graphics.clear();
				_BorderLine.graphics.lineStyle(1, 0x000000, 1);
				_BorderLine.graphics.drawRect(0, 0, cc.compositionWidth, cc.compositionHeight);
				this.addChild(_BorderLine);
				}else {
				this.removeChild(_BorderLine);
				}
			}
		
		public function get border():Boolean {
				return this.contains(_BorderLine);
			}
			
		public function addProps(text:String,parameter:String):void {
		  
		}	
		
		public function  addFace(source:Object, width:Object, height:Object):void {
			  EditManager(userTextFlow.interactionManager).insertInlineGraphic(source, width, height);
			  userTextFlow.interactionManager.setFocus();
			}
		
		public function addEventText(text:String, parameter:String,callback:Function):void {
			  _triggerText(text, parameter, 0xFF00CC).addEventListener(MouseEvent.CLICK,callback);
			}
		 
		public function movetoTextEnd():void {
			userTextFlow.interactionManager.selectRange(userTextFlow.getText().length,userTextFlow.getText().length);
			userTextFlow.interactionManager.setFocus();
			}
			
		public function  _triggerText(text:String, parameter:String,color:Object):LinkElement {
			    var SWhite:SpanElement = new SpanElement();
				    SWhite.text = " ";
                    ParagraphElement(userTextFlow.getChildAt(0)).addChild(SWhite); 
					 
				var linkspan:SpanElement = new SpanElement();
					linkspan.text = "["+text+"]";
					linkspan.color = color;
					linkspan.textDecoration = "none";
					linkspan.paddingRight=5
					 
		        var  link:LinkElement = new LinkElement();
			         link.id = parameter;
					 link.addChild(linkspan);
					 link.paragraphSpaceAfter = 200;
					 ParagraphElement(userTextFlow.getChildAt(0)).addChild(link);
					
				var EWhite:SpanElement = new SpanElement();
					EWhite.text = " ";
					ParagraphElement(userTextFlow.getChildAt(0)).addChild(EWhite); 
				    
					userTextFlow.flowComposer.updateAllControllers();
				    movetoTextEnd();
					return link;
			}
				
						
		public function parse(tf:TextFlow):String {			
			   var p:ParagraphElement = userTextFlow.getChildAt(0) as ParagraphElement;
					 for (var i:int = 0; i < p.numChildren; i++) 
					 {
						 if (p.getChildAt(i) is SpanElement) {
							 trace("/T:"+SpanElement(p.getChildAt(i)).getText())
							 }
						if (p.getChildAt(i) is InlineGraphicElement) {
							trace("/F:"+InlineGraphicElement(p.getChildAt(i)).source) 
							 }
						if (p.getChildAt(i) is LinkElement) {
						     trace("/E:"+LinkElement(p.getChildAt(i)).getText()　+ LinkElement(p.getChildAt(i)).id);
							}
					 }
					 return null
			}
	}

}